import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/Contact/Contact.dart';
import 'package:flutter_app/Contact/CreateGroup.dart';
import 'package:flutter_app/Contact/searchContact.dart';
import 'package:flutter_app/home/current_talk_widget.dart';
import 'package:flutter_app/home/identity_widget.dart';
import 'package:flutter_app/water_fall/toast.dart';
import 'package:permission_handler/permission_handler.dart';
int _selectedIndex = 0;
List<Widget> navigators = <Widget>[];

class MainPage extends StatefulWidget{
  static String username;
  static String password;

  MainPage(String username,String password){
    MainPage.username = username;
    MainPage.password = password;
  }

  @override
  State<StatefulWidget> createState() {
    return MainPageState();
  }

}

class MainPageState extends State {
  int past;
  @override
  void initState() {
    super.initState();
    _waitPermission();
    past = 0;
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '主页',
      home: WillPopScope(
        child: Scaffold(
          appBar: AppBar(
              leading: IconButton(
                icon: Icon(Icons.person_outline,size: 36,),
                onPressed: (){_onItemTap(2);},
              ),
              title: Text(MainPage.username),
              iconTheme: IconThemeData(color: Colors.black),
              actions: _selectedAppBar(_selectedIndex)
          ),
          body: _selectedWidget(_selectedIndex),
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                  icon: Icon(Icons.chat),
                  title: Text("聊天记录")
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.people),
                title: Text("联系人"),
              ),
              BottomNavigationBarItem(
                  icon: Icon(Icons.person_outline),
                  title: Text("个人中心")
              )
            ],
            currentIndex: _selectedIndex,
            onTap: _onItemTap,
          ),
        ),
        onWillPop: () async{
          int now = DateTime.now().millisecondsSinceEpoch;
          if(now - past < 500){
            exit(0);
          } else {
            past = now;
            Toast.toast(context,msg: "再按一次退出",position: ToastPosition.bottom);
          }
          return false;
        }
      )
    );
  }

  void _onItemTap(int index){
    setState(() {
      _selectedIndex = index;
      _selectedAppBar(index);
      _selectedWidget(index);
    });
  }

  //appbar按钮所在
  List<Widget> _selectedAppBar(int index){
    switch (index){
      case 0:
        //聊天记录顶部
        return <Widget>[
          //删除按钮
          IconButton(
            icon: Icon(Icons.delete_forever),
            onPressed: (){

            },
          ),
          //搜索按钮
          IconButton(
            icon: Icon(Icons.search),
            onPressed: (){},
          )
        ];
      case 1:
        //联系人顶部
        return <Widget>[
          IconButton(
            icon: Icon(Icons.delete_forever),
            onPressed: (){
              Navigator.pushNamed(context, "searchContact");
            },
          ),
          IconButton(
            icon: Icon(Icons.person_add),
            onPressed: (){
              showSearch(context: context, delegate: searchDelele());
            },
          ),
          IconButton(
            icon: Icon(Icons.group_add),
            onPressed: (){
              Navigator.push(context, new MaterialPageRoute(builder: (context)=>CreateGroup()));
            },
          ),
        ];
      case 2:
        //个人中心
        return <Widget>[
          IconButton(
            icon: Icon(Icons.exit_to_app),
            onPressed: (){
              showDialog(
                  context: context,
                  builder: (BuildContext context) =>
                      AlertDialog(
                        title: Text("注销登录"),
                        content: Text("确定注销登录"),
                        actions: <Widget>[
                          MaterialButton(
                            child :Text("取消",style: TextStyle(color: Colors.blue,fontSize: 16),),
                            onPressed: (){
                              Navigator.of(context).pop();
                            },
                          ),
                          MaterialButton(
                            child :Text("确定",style: TextStyle(color: Colors.red,fontSize: 16),),
                            onPressed: (){
                              Navigator.pushNamedAndRemoveUntil(context, "First", (route)=> route == null);
                            },
                          ),

                        ],
                      )
              );
            },
          ),
          IconButton(
            icon: Icon(Icons.settings),
            onPressed: (){
            },
          ),
        ];
    }
    return <Widget>[];
  }


  //中间布局widget
  Widget _selectedWidget(int index){

    //注册三个widget
    if(navigators.length < 3){
      navigators.add(CurrentTalkWidget());
      navigators.add(Contact());
      navigators.add(IdentityWidget());
    }

    switch (index) {
      case 0:
        //聊天记录界面
        return navigators[0];
      case 1:
        //联系人界面
        return navigators[1];
      case 2:
        //个人中心界面
        return navigators[2];
    }
    return Text("内部错误");
  }

  void _waitPermission() async{
    Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.storage]);
  }
}
